package com.example.chml7;

import java.util.Vector;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private Functions MyData[];
	private ImageView mImageView;
	private CheckBox check_zedel;
	private CheckBox check_mpi;
	private CheckBox check_libman;
	private CheckBox check_xConst;
	private CheckBox check_yConst;
	private CheckBox check_real;
	
	private SeekBar bar_x;
	private SeekBar bar_y;
	private SeekBar bar_M;
	private SeekBar bar_N;
	
	private double bx;
	private double by;
	private int bM;
	private int bN;
	
	private TextView tbx;
	private TextView tby;
	private TextView tbM;
	private TextView tbN;
	
	private int nowData = 0;
	
	private Solver solver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_main);
        
        MyData = new Functions[10];
        
        MyData[0] = new Var1();
        MyData[1] = new Var2();
        MyData[2] = new Var3();
        MyData[3] = new Var4();
        MyData[4] = new Var5();
        MyData[5] = new Var6();
        MyData[6] = new Var7();
        MyData[7] = new Var8();
        MyData[8] = new Var9();
        MyData[9] = new Var10();
          
        solver = new Solver(bN, bM);
        solver.setData(MyData[0]);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        
        mImageView = (ImageView) findViewById(R.id.imageView1);
        mImageView.setImageResource(R.drawable.t1);
        
        check_libman = (CheckBox) findViewById(R.id.checkBox_libman);
        check_mpi = (CheckBox) findViewById(R.id.checkBox_mpi);
        check_real = (CheckBox) findViewById(R.id.checkBox_real);
        
        check_xConst = (CheckBox) findViewById(R.id.checkBox_xConst);
        check_yConst = (CheckBox) findViewById(R.id.checkBox_yConst);
        check_zedel = (CheckBox) findViewById(R.id.checkBox_zedel);
        
        bar_x = (SeekBar) findViewById(R.id.seekBar_x);
        bar_y = (SeekBar) findViewById(R.id.seekBar_y);
        bar_M = (SeekBar) findViewById(R.id.seekBar_M);
        bar_N = (SeekBar) findViewById(R.id.seekBar_N);
        
        bar_x.setOnSeekBarChangeListener(seekBarChangeListener);
        bar_y.setOnSeekBarChangeListener(seekBarChangeListener);
        bar_M.setOnSeekBarChangeListener(seekBarChangeListener);
        bar_N.setOnSeekBarChangeListener(seekBarChangeListener);
        
        tbx = (TextView) findViewById(R.id.textView_bx);
        tby = (TextView) findViewById(R.id.textView_by);
        tbM = (TextView) findViewById(R.id.textView_bM);
        tbN = (TextView) findViewById(R.id.textView_bN);
        
        bx = 0.14;
        by = 0.14;
        bN = (int)(100/3);
        bM = (int)(100/3);
        
        bar_x.setProgress((int)(0.1/0.01));
        bar_y.setProgress((int)(1./0.1));
        bar_N.setProgress((int)(100/3));
        bar_M.setProgress((int)(100/3));
        
        tbx.setText(Double.toString(bx));
        tby.setText(Double.toString(by));
        tbM.setText(Integer.toString(bM));
        tbN.setText(Integer.toString(bN));
        
        check_real.setChecked(true);
        check_xConst.setChecked(true);
        
        return true;
    }
    
//    public void startPlotExp(View view){
//    	double UI[] = new double[1];
//    	double UE[] = new double[1];
//    	double uReal[] = new double[1];
//    	Vector<Double> vecEpsUI = new Vector<Double>();
//    	Vector<Double> vecEpsUE = new Vector<Double>();
//    	UI[0] = 0;
//    	UE[0] = 0;
//    	uReal[0] = 0;
//    	
//    	solver.setN(bN);
//    	solver.setM(bM);
//    	
//    	solver.setData(MyData[nowData]);
//    	
//    	double T = bt;
//    	
//    	if(check_imp.isChecked())
//    	{
//    		Log.d("test bool", "in imp");
//    		do{
//    			if(check_two_apr_1.isChecked())
//    			{
//    				Log.d("test bool", "apr_1");
//    				for(double t = 0; t < 4.0; t += 0.3)
//    				{
//    					double tmpEps = 0;
//    					UI = getU(solver.zedel());
//    					uReal = solver.uReal(t, true);
//    					for(int i = 0; i < UI.length; ++i)
//    						tmpEps += Math.abs(UI[i] - uReal[i]);
//    					vecEpsUI.addElement(tmpEps/(double)(UI.length + 1));
//    				}
//    				break;
//    			}
//    			if(check_two_apr_2.isChecked())
//    			{
//    				Log.d("test bool", "apr_2");
//    				for(double t = 0; t < 4.0; t += 0.3)
//    				{
//    					double tmpEps = 0;
//    					UI = getU(solver.mpi());
//    					uReal = solver.uReal(t, true);
//    					for(int i = 0; i < UI.length; ++i)
//    						tmpEps += Math.abs(UI[i] - uReal[i]);
//    					vecEpsUI.addElement(tmpEps/(double)(UI.length + 1));
//    				}
//    				break;
//    			}
//    			if(check_free_apr_2.isChecked())
//    			{
//    				Log.d("test bool", "apr_3");
//    				for(double t = 0; t < 4.0; t += 0.3)
//    				{
//    					double tmpEps = 0;
//    					UI = getU(solver.libman());
//    					uReal = solver.uReal(t, true);
//    					for(int i = 0; i < UI.length; ++i)
//    						tmpEps += Math.abs(UI[i] - uReal[i]);
//    					vecEpsUI.addElement(tmpEps/(double)(UI.length + 1));
//    				}
//    				break;
//    			}
//    		}while(false);
//    	}
//    		
//    	Log.d("test data", "______________________");
//    	for(int i = 0; i < UI.length; ++i)
//    	{
//    		Log.d("test data", Double.toString(UI[i]));
//    	}
//    	Log.d("test data", "______________________");
//
//    	for(int i = 0; i < UE.length; ++i)
//    	{
//    		Log.d("test data", Double.toString(UE[i]));
//    	}
//    	Log.d("test data", "______________________");
//    	if(check_real.isChecked())
//    		uReal = solver.uReal(T);
//
//    	Intent intent = new Intent(this, plotActivityExp.class);
//
//    	intent.putExtra("vecEpsUI", vecEpsUI.toArray());
//    	intent.putExtra("vecEpsUE", vecEpsUI.toArray());
//    	Vector<Double> vecT = new Vector<Double>();
//    	for(double t = 0; t < 4.0; t += 0.3)
//    		vecT.addElement(t);
//    	intent.putExtra("vecT", vecT.toArray());
//    	startActivity(intent);
//    }
    
    public void startPlotExp(View view){
    	
    	double UL[][] = new double[1][1];
    	double UZ[][] = new double[1][1];
    	double UM[][] = new double[1][1];
    	double uReal[][] = new double[1][1];
    	
    	double vecEpsUL[] = new double[bN+2];
    	double vecEpsUZ[] = new double[bN+2];
    	double vecEpsUM[] = new double[bN+2];
    	
    	vecEpsUL[0] = -100.;
    	vecEpsUM[0] = -100.;
    	vecEpsUZ[0] = -100.;
    	
    	UL[0][0] = 0;
    	UZ[0][0] = 0;
    	UM[0][0] = 0;
    	uReal[0][0] = 0;
    	
    	solver.setN(bN);
    	solver.setM(bM);
    	
    	solver.setData(MyData[nowData]);
    	
        solver.setCX(bx);
        solver.setCY(by);
        
        if(check_xConst.isChecked())
        	solver.setConstVal('x');
        if(check_yConst.isChecked())
        	solver.setConstVal('y');
    	
        uReal = solver.uReal();
        
        double uReale[] = getU(uReal, check_xConst.isChecked());
        
    	if(check_libman.isChecked())
    	{
    		Log.d("test bool", "in lib");
    		UL = solver.libman();
    		double ULe[] = getU(UL, check_xConst.isChecked());
    		double tmpEps = 0;
			for(int i = 0; i < ULe.length; ++i)
			{
				tmpEps = Math.abs(ULe[i] - uReale[i]);
				vecEpsUL[i] = tmpEps;
			}
    	}
    	
    	if(check_zedel.isChecked())
    	{
    		Log.d("test bool", "in lib");
    		UZ = solver.zedel();
    		double UZe[] = getU(UZ, check_xConst.isChecked());
    		double tmpEps = 0;
			for(int i = 0; i < UZe.length; ++i)
			{
				tmpEps = Math.abs(UZe[i] - uReale[i]);
				vecEpsUZ[i] = tmpEps;
			}
    	}
    	
    	if(check_mpi.isChecked())
    	{
    		Log.d("test bool", "in lib");
    		UM = solver.mpi();
    		double UMe[] = getU(UM, check_xConst.isChecked());
    		double tmpEps = 0;
			for(int i = 0; i < UMe.length; ++i)
			{
				tmpEps = Math.abs(UMe[i] - uReale[i]);
				vecEpsUM[i] = tmpEps;
			}
    	}
    	
//    	if(check_real.isChecked())
//    	{
//    		uReal = solver.uReal();
//    	}
    	
    	Intent intent = new Intent(this, plotActivityExp.class);
    	
    	
    	intent.putExtra("vecEpsUM", vecEpsUM);
    	intent.putExtra("vecEpsUL", vecEpsUL);
    	intent.putExtra("vecEpsUZ", vecEpsUZ);
    	
    	if(check_xConst.isChecked())
    	{
        	double vecT[] = new double[bN+2];
        	int i = 0;
        	for(double y = 0; y <= MyData[nowData].Ly; y += solver.getH2())
        		vecT[i++] = y;
        	intent.putExtra("vecT", vecT);
    	}
    	else
    	{
    		double vecT[] = new double[bN+2];
    		int i = 0;
        	for(double x = 0; x <= MyData[nowData].Lx; x += solver.getH1())
        		vecT[i++] = x;
        	intent.putExtra("vecT", vecT);
    	}
    	
    	startActivity(intent);
    }
    
    private double[] getU(double U[][], boolean xConst)
    {
    	if(U.length < 2)
    	{
    		double U1[] = new double[1];
    		U1[0] = 0;
    		return U1;
    	}
    	
    	double U1[] = new double[solver.getN()];
    	
    	double h;
    	int j;
    	if(xConst)
    	{
    		h = solver.getH1();
    		j = (int)(bx/h);
    	}
    	else
    	{
    		h = solver.getH2();
    		j = (int)(by/h);
    	}
    	
    	for(int i = 0; i < solver.getN(); ++i)
    	{
    		if(xConst)
    			U1[i] = U[j][i];
    		else
    			U1[i] = U[i][j];
    	}
    	
    	Log.d("U1 size", Integer.toString(U1.length));
    	
    	return U1;
    }
    
    public void startPlot(View view){
    	
    	double UL[][] = new double[1][1];
    	double UZ[][] = new double[1][1];
    	double UM[][] = new double[1][1];
    	double uReal[][] = new double[1][1];
    	UL[0][0] = 0;
    	UZ[0][0] = 0;
    	UM[0][0] = 0;
    	uReal[0][0] = 0;
    	
    	solver.setN(bN);
    	solver.setM(bM);
    	
    	solver.setData(MyData[nowData]);
    	
        solver.setCX(bx);
        solver.setCY(by);
        
        if(check_xConst.isChecked())
        	solver.setConstVal('x');
        if(check_yConst.isChecked())
        	solver.setConstVal('y');
    	
    	if(check_libman.isChecked())
    	{
    		Log.d("test bool", "in lib");
    		UL = solver.libman();
    	}
    	
    	if(check_zedel.isChecked())
    	{
    		Log.d("test bool", "in lib");
    		UZ = solver.zedel();
    	}
    	
    	if(check_mpi.isChecked())
    	{
    		Log.d("test bool", "in lib");
    		UM = solver.mpi();
    	}
    	
    	if(check_real.isChecked())
    	{
    		uReal = solver.uReal();
    	}
    	
    	Intent intent = new Intent(this, plotActivity.class);
    	
    	intent.putExtra("uReal", getU(uReal, check_xConst.isChecked()));
    	intent.putExtra("UL", getU(UL, check_xConst.isChecked()));
    	intent.putExtra("UM", getU(UM, check_xConst.isChecked()));
    	intent.putExtra("UZ", getU(UZ, check_xConst.isChecked()));
    	
    	if(check_xConst.isChecked())
    	{
    		intent.putExtra("x0", MyData[nowData].y0);
    		intent.putExtra("L", MyData[nowData].Ly);
    		intent.putExtra("h", solver.getH2());
    	}
    	else
    	{
    		intent.putExtra("x0", MyData[nowData].x0);
    		intent.putExtra("L", MyData[nowData].Lx);
    		intent.putExtra("h", solver.getH1());
    	}
    	
    	startActivity(intent);
    }
    
    private void setImage()
    {
    	switch(nowData)
    	{
    	case 0:
    		mImageView.setImageResource(R.drawable.t1);
    		break;
    	case 1:
    		mImageView.setImageResource(R.drawable.t2);
    		break;
    	case 2:
    		mImageView.setImageResource(R.drawable.t3);
    		break;
    	case 3:
    		mImageView.setImageResource(R.drawable.t4);
    		break;
    	case 4:
    		mImageView.setImageResource(R.drawable.t5);
    		break;
    	case 5:
    		mImageView.setImageResource(R.drawable.t6);
    		break;
    	case 6:
    		mImageView.setImageResource(R.drawable.t7);
    		break;
    	case 7:
    		mImageView.setImageResource(R.drawable.t8);
    		break;
    	case 8:
    		mImageView.setImageResource(R.drawable.t9);
    		break;
    	case 9:
    		mImageView.setImageResource(R.drawable.t10);
    		break;
    	}
    	solver.setData(MyData[nowData]);
    }
    
    public void prevData(View view){
    	--nowData;
    	if(nowData == -1)
    		nowData = 9;
    	setImage();
    }
    
    public void nextData(View view){
    	++nowData;
    	if(nowData == 10)
    		nowData = 0;
    	setImage();
    }
    
    private SeekBar.OnSeekBarChangeListener seekBarChangeListener = 
		    new SeekBar.OnSeekBarChangeListener() 
    {
		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			// TODO Auto-generated method stub
			updateSeekBars();
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
		}
    };
    
    private void updateSeekBars()
    {
    	bx = (double)(bar_x.getProgress()*solver.getLx()) / 100;
    	by = (double)(bar_y.getProgress()*solver.getLy()) / 100;
    	bM = (int)(bar_M.getProgress()) * 3;
    	bN = (int)(bar_N.getProgress()) * 3;
    	
        tbx.setText(Double.toString(bx));
        tby.setText(Double.toString(by));
        tbM.setText(Integer.toString(bM));
        tbN.setText(Integer.toString(bN));
    }
}

